#include "gamedata.h"

#include <sstream>

Gamedata& Gamedata::getInstance()
{
	if ( instance == NULL ) instance = new Gamedata;
	return *instance;
}

Gamedata::Gamedata(const string& fn ) :
	parser(fn),
	gameData(parser.getXmlData())
{
	
}

bool Gamedata::getXmlBool(const string& tag) const
{
	std::map<string, string>::const_iterator ptr = gameData.find(tag);
	if ( ptr == gameData.end() )
		throw string("Game: Didn't find boolean tag ")+tag+string(" in xml");
	else {
		if ( ptr->second == "true" ) return true;
		else return false;
	}
}

int Gamedata::getXmlInt(const string& tag) const
{
	std::map<string, string>::const_iterator ptr = gameData.find(tag);
	if ( ptr == gameData.end() )
		throw string("Game: Didn't find integer tag ")+tag+string(" in xml");
	else {
		std::stringstream strm;
		strm << ptr->second;
		int data;
		strm >> data;
		return data;
	}
}

float Gamedata::getXmlFloat(const string& tag) const
{
	std::map<string, string>::const_iterator ptr = gameData.find(tag);
	if ( ptr == gameData.end() )
		throw string("Game: Didn't find float tag ")+tag+string(" in xml");
	else {
		std::stringstream strm;
		strm << ptr->second;
		int data;
		strm >> data;
		return data;
	}
}

const string& Gamedata::getXmlStr(const string& tag) const
{
	std::map<string, string>::const_iterator ptr = gameData.find(tag);
	if ( ptr == gameData.end() )
		throw string("Game: Didn't find string tag ")+tag+string(" in xml");
	else return ptr->second;
}

void Gamedata::displayData() const
{
	std::map<string, string>::const_iterator ptr = gameData.begin();
	while ( ptr != gameData.end() ) {
		cout << ptr->first << ", " << ptr->second << endl;
		++ptr;
	}
}


